<?php
class power extends core {
	public function __construct() {
		parent::__construct();
	}

	public function index(){
		$keyword = (isset($_GET['keyword']) && trim($_GET['keyword'])) ? trim($_GET['keyword']) : '';
		$where = '';
		if ($keyword) {
			$where = "AND name LIKE '%{$keyword}%' ";
		}
		$sql = "SELECT * FROM {$this->tbp}admin WHERE 1 {$where} ORDER BY id DESC";
		$this->setpages(array('keyword'=>$keyword));
		$row = $this->ezr->get_results($sql);

		$this->smarty->assign('rs',$row);
		$nav = $this->ezr->get_navigation();
		$this->smarty->assign('page',$nav);
		$this->smarty->assign('keyword',$keyword);
		$this->display();
	}
	
	public function edit(){
		if (IS_POST) {
			$this->save();
		}else{
			//读出权限
			// for( $i=1 ; $i<=66 ; $i++){
			// 	$sql = "INSERT INTO {$this->tbp}admin_menu( menu_id , admin_id , add_time  ) VALUES
			// 	( '{$i}' , 7 , '".time()."' )";
			// 	$this->db->query($sql);
			// }
			$id = (isset($_GET['id']) && trim($_GET['id'])) ? intval($_GET['id']) : 0;
			$sql = "SELECT * FROM {$this->tbp}admin_menu WHERE admin_id = '{$id}'";
			$menu = $this->db->get_results($sql);
			######//读出菜单
			$p = $this->menu($id);
			//var_dump($p);exit();
			// print_r($pa);exit;
			######

			$sql = "SELECT * FROM {$this->tbp}admin WHERE id='{$id}'";
			$row = $this->db->get_row($sql);

			$index_permission	= $this->permission("index");
			$add_permission		= $this->permission("add");
			$edit_permission	= $this->permission("edit");
			$delete_permission	= $this->permission("delete");
			$other_permission	= $this->permission("other");

			$index	= $this->search($id, "index");
			$add	= $this->search($id, "add");
			$edit	= $this->search($id, "edit");
			$delete	= $this->search($id, "delete");
			$other	= $this->search($id, "other");
			// var_dump($index);exit;

			$this->smarty->assign('row', $row);
			$this->smarty->assign('menu', $menu);
			$this->smarty->assign('pa', $p);
			
			$this->smarty->assign('index', $index);
			$this->smarty->assign('add', $add);
			$this->smarty->assign('edit', $edit);
			$this->smarty->assign('delete', $delete);
			$this->smarty->assign('other', $other);

			//die(json_encode($other));
			
			$this->smarty->assign('index_permission', $index_permission);
			$this->smarty->assign('add_permission', $add_permission);
			$this->smarty->assign('edit_permission', $edit_permission);
			$this->smarty->assign('delete_permission', $delete_permission);
			$this->smarty->assign('other_permission', $other_permission);

			$this->display();
		}
	}

	public function save(){
		$id = (isset($_POST['id']) && intval($_POST['id'])) ? intval($_POST['id']) : 0;
		$index = (isset($_POST['index']) && $_POST['index']) ? $_POST['index'] : '';
		$add = (isset($_POST['add']) && $_POST['add']) ? $_POST['add'] : '';
		$edit = (isset($_POST['edit']) && $_POST['edit']) ? $_POST['edit'] : '';
		$delete = (isset($_POST['delete']) && $_POST['delete']) ? $_POST['delete'] : '';
		$other = (isset($_POST['other']) && $_POST['other']) ? $_POST['other'] : '';
		$status = (isset($_POST['status']) && trim($_POST['status'])) ? intval($_POST['status']) : 0;
		$menu = (isset($_POST['menu'])) ? $_POST['menu'] : 0;
		// var_dump($index);
		// var_dump($add);
		// var_dump($edit);
		// var_dump($delete);exit;
		// var_dump($other);exit;
		//菜单权限
		if ($menu) {
			$sql = "DELETE FROM {$this->tbp}admin_menu WHERE admin_id = '{$id}'";
			$this->db->query($sql);
			foreach ($menu as $key => $val) {
				$sql = "INSERT INTO {$this->tbp}admin_menu (menu_id , admin_id , add_time) VALUES ('{$val}' , '{$id}' ,'".time()."')";
				$this->db->query($sql);
			}
		}
		//删除原有的
		$sql = "DELETE FROM {$this->tbp}admin_permission WHERE admin_id = '{$id}'";
		$this->db->query($sql);
		//列表权限
		if ($index) {
			$this->set($index , $id , "index");
		}
		//添加权限
		if ($add) {
			$this->set($add , $id , "add");
		}
		//编辑权限
		if ($edit) {
			$this->set($edit , $id , "edit");
		}
		//删除权限
		if ($delete) {
			$this->set($delete , $id , "delete");
		}
		//其它
		if ($other) {
			$this->set($other , $id , "other");
		}
		$sql = "UPDATE {$this->tbp}admin SET status='{$status}' WHERE id='{$id}'";
		$this->db->query($sql);
		header("Location:?app=power&act=index");
	}

	private function set($arr , $id , $act){
		foreach ($arr as $k => $val) {
			$v = $val;
			$c = $act;
			if (strpos($val, '|')!==false) {
				$vals = explode('|', $val);
				$v = $vals[0];
				$c = $vals[1];
			}
			$sql = "INSERT INTO {$this->tbp}admin_permission (admin_id, app, act) VALUES ('{$id}', '{$v}', '{$c}')";
			$this->db->query($sql);
		}
		return true;
	}
	
	//组装一维数组
	private function make_arr($arr, $act){
		$a = array();
		if ($arr) {
			foreach ($arr as $key => $val) {
				$a[] = $act=='other' ? $val->act : $val->app;
			}
		}
		return $a;
	}
	
	//搜索数据
	public function search($id, $act){
		$where = '';
		if ($act=='other') {
			$where .= " AND act NOT IN ('index', 'add', 'edit', 'delete', '*')";
		} else {
			$where .= " AND act='{$act}'";
		}
		$sql = "SELECT * FROM {$this->tbp}admin_permission WHERE admin_id='{$id}' {$where}";
		$row = $this->make_arr($this->db->get_results($sql), $act);
		return $row;
	}
	
	//权限数据集
	public function permission($act){
		$where = '';
		if ($act=='other') {
			$where .= " AND act NOT IN ('index', 'add', 'edit', 'delete')";
		} else {
			$where .= " AND act='{$act}'";
		}
		$sql = "SELECT * FROM {$this->tbp}gm_permission WHERE status=1 {$where} ORDER BY sort ASC, id ASC";
		$row = $this->db->get_results($sql);
		//if ($act=='other') die(json_encode($row));
		return $row;
	}
}
